﻿using System;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using XNAGameEngine;
using XNAGameEngine.GameState;
using XNAGameEngine.GUI;
using XNAGameEngine.Input;
using XNAGameEngine.GameObject;
using XNAGameEngine.Audio;
using XNAGameEngine.Alarms;

namespace XNAGame
{
    class NewGameState : GameState, IAlarmListener
    {
        Texture2D bg;
        AlarmSystem bg_alarm;

        public NewGameState(string stringReference, IManagerGameState stateManager) 
            : base(stringReference, stateManager)
        {
            bg_alarm = new AlarmSystem(this);
        }

        public override void Initialize()
        {
            base.Initialize();

            GameEngine.GetService<IManagerGUI>().LoadGUILayout("NewGameGUI");

            bg = GameEngine.GameContent.Load<Texture2D>("Content\\Textures\\NGbg_1");

            bg_alarm.Add(new AlarmObject(this, 10.0, "bg2_alarm"));
            bg_alarm.Add(new AlarmObject(this, 25.0, "bg3_alarm"));
            bg_alarm.Add(new AlarmObject(this, 40.0, "bg4_alarm"));
            bg_alarm.Add(new AlarmObject(this, 50.0, "bg5_alarm"));

            GameEngine.GetService<IManagerAudio2D>().NextSong();
        }

        public override void Cleanup()
        {
            GameEngine.GetService<IManagerGUI>().UnloadGUILayout();
            base.Cleanup();
        }

        public override void Update(GameTime gameTime)
        {
            bg_alarm.Update(gameTime);
            base.Update(gameTime);
        }

        public override void Draw(GameTime gameTime)
        {
            GameEngine.SpriteBatch.Begin();

            GameEngine.SpriteBatch.Draw(
                bg,
                new Rectangle(0, 0, bg.Width,
                bg.Height), null,
                Color.White, 0, Vector2.Zero,
                SpriteEffects.None, 0); 

            GameEngine.SpriteBatch.End();

            base.Draw(gameTime);
        }

        #region IAlarmListener Members

        public void OnAlarmEvent(string message)
        {
            switch (message)
            {
                case "bg2_alarm": bg = GameEngine.GameContent.Load<Texture2D>("Content\\Textures\\NGbg_2");
                    break;

                case "bg3_alarm": bg = GameEngine.GameContent.Load<Texture2D>("Content\\Textures\\NGbg_3");
                    break;

                case "bg4_alarm": bg = GameEngine.GameContent.Load<Texture2D>("Content\\Textures\\NGbg_4");
                    break;

                case "bg5_alarm": bg = GameEngine.GameContent.Load<Texture2D>("Content\\Textures\\NGbg_5");
                    break;
            }
        }

        #endregion
    }
}
